package com.songtech.server.celltrack.service;

import com.songtech.server.celltrack.model.CellTrack;
import com.songtech.server.celltrack.util.TransUtil;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @Des 基站定位
 * Created by wxq on 2018/4/17.
 */
//@Service
public class CellTrackService {

    /**
     * 通过Lac和cell_id查询经度（longitude）和纬度(latitude)
     * @param lac LAC号码
     * @param cell_id cell_id号码
     * @return CellTrack实体：getLongitude --> 经度
     *                         getLatitude --> 纬度
     *                         getAddress --> 地址
     */
    public static CellTrack queryCellTrackInfo(String lac, String cell_id) throws IOException {

        String url = "http://www.cellid.cn/m/cidInfo.php";

        StringBuffer sb= TransUtil.getContent(url,lac,cell_id);

        //地址被<br>标签包裹
        int a = sb.indexOf("<br>") + "<br>".length();
        int b = sb.lastIndexOf("<br>");
        //经纬度被coord和addressInfo包裹
        int c = sb.indexOf("coord=") + "coord=".length();
        int d = sb.lastIndexOf("&addressInfo");
        //获取经纬度
        String jw = sb.substring(c,d);

        String address = sb.substring(a,b); //地址
        String latitude = jw.substring(0,jw.indexOf(",")); //纬度
        String longitude = jw.substring(jw.indexOf(",") + 1); //经度

        CellTrack cellTrack = new CellTrack();
        cellTrack.setLongitude(new BigDecimal(longitude));
        cellTrack.setLatitude(new BigDecimal(latitude));
        cellTrack.setAddress(address);

        return cellTrack;
    }


}
